package com.feige.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.base.dto.BaseDto;
import com.feige.base.po.IPersistence;
import com.feige.base.service.BaseService;
import com.feige.base.vo.BaseVo;
import com.feige.web.result.ListResult;
import com.feige.web.result.PageInfo;
import com.feige.web.result.PageResult;
import com.feige.web.result.SingleResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;


import java.util.List;
import java.util.stream.Collectors;

/**
 * @author feige
 */
public abstract class BaseController<S extends BaseService<T>, T extends IPersistence, D extends BaseDto<T>> {

    protected S baseService;


    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个实体")
    public SingleResult<BaseVo> getById(@PathVariable("id") Long id){
        T t = baseService.getById(id);
        if (t != null){
            return SingleResult.success(t.toVo());
        }
        return SingleResult.empty();
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public PageResult<T, BaseVo> page(PageInfo<T> pageInfo, D dto){
        LambdaQueryWrapper<T> queryWrapper = dto.baseQueryWrapper();
        Page<T> tPage = pageInfo.toMpPage();
        baseService.page(tPage, queryWrapper);
        pageInfo.fill(tPage);
        List<BaseVo> voList = tPage.getRecords()
                .stream()
                .map(IPersistence::toVo)
                .collect(Collectors.toList());
        return PageResult.success(pageInfo, voList);
    }


    @PostMapping("/ids")
    @ApiOperation(value = "查询多个实体")
    public ListResult<BaseVo> getByIds(@RequestBody List<Long> ids){
        List<T> ts = baseService.listByIds(ids);
        if (CollectionUtils.isEmpty(ts)){
            return ListResult.success();
        }
        return ListResult.success(ts.stream()
                .map(IPersistence::toVo)
                .collect(Collectors.toList())
        );
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个实体")
    public SingleResult<BaseVo> deleteById(@PathVariable("id") Long id){
        return SingleResult.isOk(baseService.removeById(id));
    }


    @PutMapping("/ids")
    @ApiOperation(value = "删除多个实体")
    public SingleResult<BaseVo> deleteByIds(@RequestBody List<Long> ids){
        return SingleResult.isOk(baseService.removeByIds(ids));
    }


    @PostMapping
    @ApiOperation(value = "保存")
    public SingleResult<BaseVo> save(@RequestBody @Validated D dto){
        return SingleResult.isOk(baseService.save(dto.toPo()));
    }

    @PutMapping
    @ApiOperation(value = "更新")
    public SingleResult<BaseVo> update(@RequestBody @Validated D dto){
        return SingleResult.isOk(baseService.updateById(dto.toPo()));
    }


    @PostMapping("/saveOrUpdateBatch")
    @ApiOperation(value = "批量保存或更新")
    public SingleResult<BaseVo> saveOrUpdateBatch(@RequestBody List<D> ds){
        List<T> list = ds.stream()
                .map(BaseDto::toPo)
                .collect(Collectors.toList());
        return SingleResult.isOk(baseService.saveOrUpdateBatch(list));
    }

}
